package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.ApAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.article.dtos.AuthorDto;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP文章作者信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-10-19
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {

    @Override
    public Integer saveAuthor(AuthorDto dto) {

        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getUserId,dto.getUserId());
        ApAuthor apAuthor = getOne(queryWrapper);
        if(apAuthor==null){
//            新增
            apAuthor = new ApAuthor();
            apAuthor.setUserId(dto.getUserId());
            apAuthor.setWmUserId(dto.getWmUserId());
            apAuthor.setCreatedTime(new Date());
        }
        apAuthor.setName(dto.getName());
        apAuthor.setType(dto.getType());
//        如果存在就更新，如果不存在就新增
        boolean b = saveOrUpdate(apAuthor);


//        try{
//            Thread.sleep(5000);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        return apAuthor.getId();

    }

    @Override
    public ApAuthor findByWmUserId(Integer wmUserId) {


        LambdaQueryWrapper<ApAuthor> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ApAuthor::getWmUserId,wmUserId );
        ApAuthor one = getOne(lqw);



        return one;
    }
}
